package org.zz.mxhidfingerdriver;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Handler;
import android.util.Log;
import java.util.HashMap;
import java.util.Iterator;
import org.zz.protocol.MXLog;

public class UsbBase {
    private int m_iSendPackageSize = 0;
    private int m_iRecvPackageSize = 0;
    private UsbDevice m_usbDevice = null;
    private UsbInterface m_usbInterface = null;
    private UsbEndpoint m_inEndpoint = null;
    private UsbEndpoint m_outEndpoint = null;
    private UsbDeviceConnection m_connection = null;
    private Context m_ctx = null;
    private Handler m_fHandler = null;
    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if("com.android.example.USB_PERMISSION".equals(action)) {
                synchronized(this) {
                    UsbDevice device1 = (UsbDevice)intent.getParcelableExtra("device");
                    if(!intent.getBooleanExtra("permission", false)) {
                        Log.d("MIAXIS", "permission denied for device " + device1);
                    }
                }
            }

            if("android.hardware.usb.action.USB_DEVICE_DETACHED".equals(action)) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra("device");
                if(device != null) {
                    UsbBase.this.m_connection.releaseInterface(UsbBase.this.m_usbInterface);
                    UsbBase.this.m_connection.close();
                }
            }

        }
    };

    public UsbBase(Context context) {
        this.m_ctx = context;
        this.m_fHandler = null;
        this.regUsbMonitor();
    }

    public UsbBase(Context context, Handler bioHandler) {
        this.m_ctx = context;
        this.m_fHandler = bioHandler;
        MXLog.SetHandler(bioHandler);
        this.regUsbMonitor();
    }

    public String getDevAttribute() {
        if(this.m_ctx == null) {
            return null;
        } else {
            int iDevNum = 0;
            String strDevInfo = "";
            String strTmp = null;
            UsbManager usbManager = (UsbManager)this.m_ctx.getSystemService(Context.USB_SERVICE);
            HashMap map = usbManager.getDeviceList();
            Iterator var7 = map.values().iterator();

            while(var7.hasNext()) {
                UsbDevice device = (UsbDevice)var7.next();
                if(!usbManager.hasPermission(device)) {
                    PendingIntent pi = PendingIntent.getBroadcast(this.m_ctx, 0, new Intent("com.android.example.USB_PERMISSION"), 0);
                    usbManager.requestPermission(device, pi);
                    return null;
                }

                ++iDevNum;
                strTmp = "describeContents:" + device.describeContents();
                strDevInfo = strDevInfo + strTmp + "\r\n";
                strTmp = "DeviceProtocol:" + device.getDeviceProtocol();
                strDevInfo = strDevInfo + strTmp + "\r\n";
                strTmp = "DeviceClass:" + device.getDeviceClass();
                strDevInfo = strDevInfo + strTmp + "\r\n";
                strTmp = "DeviceSubclass:" + device.getDeviceSubclass();
                strDevInfo = strDevInfo + strTmp + "\r\n";
                strTmp = "InterfaceCount:" + device.getInterfaceCount();
                strDevInfo = strDevInfo + strTmp + "\r\n";
                strTmp = "DeviceId:" + device.getDeviceId();
                strDevInfo = strDevInfo + strTmp + "\r\n";
                strTmp = "DeviceName:" + device.getDeviceName();
                strDevInfo = strDevInfo + strTmp + "\r\n";
                strTmp = "VendorId:" + device.getVendorId();
                strDevInfo = strDevInfo + strTmp + "\r\n";
                strTmp = "ProductId:" + device.getProductId();
                strDevInfo = strDevInfo + strTmp + "\r\n";
                MXLog.SendMsg("Device\r\n" + strDevInfo);
            }

            return strDevInfo;
        }
    }

    public int getDevNum(int vid, int pid) {
        if(this.m_ctx == null) {
            return -102;
        } else {
            int iDevNum = 0;
            UsbManager usbManager = (UsbManager)this.m_ctx.getSystemService(Context.USB_SERVICE);
            HashMap map = usbManager.getDeviceList();
            Iterator var7 = map.values().iterator();

            while(var7.hasNext()) {
                UsbDevice device = (UsbDevice)var7.next();
                if(!usbManager.hasPermission(device)) {
                    PendingIntent pi = PendingIntent.getBroadcast(this.m_ctx, 0, new Intent("com.android.example.USB_PERMISSION"), 0);
                    usbManager.requestPermission(device, pi);
                    return -101;
                }

                if(vid == device.getVendorId() && pid == device.getProductId()) {
                    ++iDevNum;
                }
            }

            return iDevNum;
        }
    }

    public int openDev(int vid, int pid) {
        MXLog.SendMsg("openDev");
        if(this.m_ctx == null) {
            MXLog.SendMsg("ERRCODE_NO_CONTEXT");
            return -102;
        } else {
            MXLog.SendMsg("vid: " + vid + "\t pid: " + pid);
            UsbManager usbManager = (UsbManager)this.m_ctx.getSystemService(Context.USB_SERVICE);
            HashMap map = usbManager.getDeviceList();
            Iterator var6 = map.values().iterator();

            while(var6.hasNext()) {
                UsbDevice device = (UsbDevice)var6.next();
                if(!usbManager.hasPermission(device)) {
                    PendingIntent pi = PendingIntent.getBroadcast(this.m_ctx, 0, new Intent("com.android.example.USB_PERMISSION"), 0);
                    usbManager.requestPermission(device, pi);
                    return -101;
                }

                MXLog.SendMsg("dName: " + device.getDeviceName());
                MXLog.SendMsg("vid: " + device.getVendorId() + "\t pid: " + device.getProductId());
                if(vid == device.getVendorId() && pid == device.getProductId()) {
                    this.m_usbDevice = device;
                    this.m_usbInterface = this.m_usbDevice.getInterface(0);
                    this.m_inEndpoint = this.m_usbInterface.getEndpoint(0);
                    this.m_outEndpoint = this.m_usbInterface.getEndpoint(1);
                    this.m_connection = usbManager.openDevice(this.m_usbDevice);
                    this.m_connection.claimInterface(this.m_usbInterface, true);
                    this.m_iSendPackageSize = this.m_outEndpoint.getMaxPacketSize();
                    this.m_iRecvPackageSize = this.m_inEndpoint.getMaxPacketSize();
                    MXLog.SendMsg("-------------------------------------------");
                    MXLog.SendMsg("SendPackageSize: " + this.m_iSendPackageSize + ",RecvPackageSize: " + this.m_iRecvPackageSize);
                    return 0;
                }
            }

            return -100;
        }
    }

    public int sendPacketSize() {
        return this.m_iSendPackageSize;
    }

    public int recvPacketSize() {
        return this.m_iRecvPackageSize;
    }

    public int clearBuffer() {
        boolean iRV = false;
        int iEndpointSize = this.recvPacketSize();
        byte[] bRecvBufTmp = new byte[iEndpointSize];

        while(true) {
            int iRV1 = this.m_connection.bulkTransfer(this.m_inEndpoint, bRecvBufTmp, iEndpointSize, 5);
            if(iRV1 < 0) {
                return 0;
            }

            MXLog.SendMsg("clearBuffer iRV=" + iRV1);
        }
    }

    public int sendData(byte[] bSendBuf, int iSendLen, int iTimeOut) {
        boolean iRV = true;
        if(iSendLen > bSendBuf.length) {
            return -18;
        } else {
            int iPackageSize = this.sendPacketSize();
            if(iSendLen > iPackageSize) {
                return -18;
            } else {
                byte[] bSendBufTmp = new byte[iPackageSize];
                System.arraycopy(bSendBuf, 0, bSendBufTmp, 0, iSendLen);
                int iRV1 = this.m_connection.bulkTransfer(this.m_outEndpoint, bSendBufTmp, iPackageSize, iTimeOut);
                return iRV1;
            }
        }
    }

    public int recvData(byte[] bRecvBuf, int iRecvLen, int iTimeOut) {
        int iRV = -1;
        if(iRecvLen > bRecvBuf.length) {
            return -18;
        } else {
            int iPackageSize = this.recvPacketSize();
            byte[] bRecvBufTmp = new byte[iPackageSize];

            for(int i = 0; i < iRecvLen; i += iPackageSize) {
                int nDataLen = iRecvLen - i;
                if(nDataLen > iPackageSize) {
                    nDataLen = iPackageSize;
                }

                iRV = this.m_connection.bulkTransfer(this.m_inEndpoint, bRecvBufTmp, nDataLen, iTimeOut);
                if(iRV < 0) {
                    return iRV;
                }

                System.arraycopy(bRecvBufTmp, 0, bRecvBuf, i, iRV);
            }

            return iRV;
        }
    }

    public int closeDev() {
        if(this.m_connection != null) {
            this.m_connection.releaseInterface(this.m_usbInterface);
            this.m_connection.close();
            this.m_connection = null;
        }

        return 0;
    }

    private void regUsbMonitor() {
        IntentFilter filter = new IntentFilter("com.android.example.USB_PERMISSION");
        this.m_ctx.registerReceiver(this.mUsbReceiver, filter);
    }

    public void unRegUsbMonitor() {
        this.m_ctx.unregisterReceiver(this.mUsbReceiver);
    }
}
